<template>
	<view class="">
		<!-- 顶部导航栏 -->
		<view class="top-navbar">
			<view class="container-fluid">
				<view class="row align-items-center">
					<view class="col-lg-3 col-md-3 col-sm-12">
						<view class="logo">
							<image src="/static/petLogo.png" mode="aspectFit"></image>
							<text class="titleText">宠物健康管家</text>
						</view>
					</view>
					<view class="col-lg-6 col-md-6 col-sm-12">
						<view class="navbar-center">
							<!-- 分类导航移到这里 -->
							<view class="nav-categories">
								<view class="nav-item" @click="goIndex">
									<text class="titleText">首页</text>
								</view>
								<view class="nav-item" @click="goHealth">
									<text class="titleText">健康</text>
								</view>
								<view class="nav-item" @click="goShop">
									<text class="titleText">商城</text>
								</view>
								<view class="nav-item" @click="goForum">
									<text class="titleText">论坛</text>
								</view>
							</view>
							<!-- 搜索栏缩短 -->
							<view class="search-container">
								<image src="/static/index/search-icon.png" mode="aspectFill"></image>
								<input type="text" placeholder="搜索宠物食品" class="search-input" />
								<button class="search-btn">搜索</button>
							</view>
						</view>
					</view>
					<view class="col-lg-3">
						<view class="user-actions">
							<view class="action-item" @click="goMine">
								<image src="/static/Tabbar/mine.png" style="width: 60rpx;height: 60rpx;"
									mode="aspectFill"></image>
								<text class="titleText">我的账户</text>
							</view>
							<view class="action-item" @click="goShopCar">
								<image src="/static/Tabbar/shop.png" style="width: 60rpx;height: 60rpx;"
									mode="aspectFill"></image>
								<text class="titleText">购物车</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="page-container">
			<!-- 个人信息卡片 -->
			<view class="info-container">
				<view class="info-content">
					<!-- 左侧头像部分 -->
					<view class="avatar-section">
						<image class="avatar" :src="userInfo.avatar" @click="chooseAvatar"></image>
						<text class="edit-avatar">修改头像</text>
					</view>

					<!-- 右侧表单部分 -->
					<view class="form-section">
						<view class="form-item">
							<text class="label">昵称</text>
							<input class="input" v-model="userInfo.username" placeholder="请输入昵称" />
						</view>

						<view class="form-item">
							<text class="label">签名</text>
							<input class="input" v-model="userInfo.introduce" placeholder="请输入个性签名" />
						</view>

						<view class="form-item">
							<text class="label">手机号</text>
							<input class="input" v-model="userInfo.phone" placeholder="请输入手机号" />
						</view>
					</view>
				</view>

				<!-- 底部保存按钮 -->
				<view class="button-section">
					<button class="save-btn" @click="saveUserInfo">保存修改</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import titleBar from '@/components/titleBar.vue'
	import {
		userApi
	} from '@/api/user';

	export default {
		components: {
			titleBar
		},
		data() {
			return {
				globaApi: '',
				userInfo: {
					avatar: '',
					username: '',
					introduce: '',
					phone: '',
				},
				token: ''
			};
		},
		mounted() {
			this.getUserInfo()
		},
		methods: {
			// 获取用户信息
			async getUserInfo() {
				try {
					const result = await userApi.getUserInfo()
					this.userInfo.username = result.username
					this.userInfo.avatar = result.avatar
					this.userInfo.introduce = result.introduce
					this.userInfo.phone = result.phone
					console.log('获取用户信息成功：', result);
				} catch (error) {
					console.error('获取用户信息失败：', error);
				}
			},
			// 选择头像
			async chooseAvatar() {
				try {
					const {
						tempFilePaths
					} = await uni.chooseImage({
						count: 1, // 一次只选择一张图片
						sizeType: ['compressed'], // 压缩图片，减少上传数据量
						sourceType: ['album', 'camera'] // 支持从相册选择或使用相机拍照
					});
					// 判断是否选择了图片
					if (tempFilePaths && tempFilePaths.length > 0) {
						// 显示上传中提示
						uni.showLoading({
							title: '上传中...'
						});

						// 2. 调用上传头像接口
						const result = await userApi.uploadAvatar(tempFilePaths[0]);
						console.log(result);

						// 3. 更新用户信息
						// 3. 更新用户信息
						// 更新本地头像 URL
						this.userInfo.avatar = result[0];
						// 更新用户信息
						await this.saveUserInfo();
						await this.getUserInfo();

						// 提示上传成功
						uni.showToast({
							title: '头像更新成功'
						});
					}
				} catch (error) {
					console.error('上传头像失败:', error);
					uni.hideLoading();
					uni.showToast({
						title: '上传失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
				}
			},

			// 保存用户信息
			async saveUserInfo() {
				// 表单验证
				if (!this.userInfo.username.trim()) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}

				// 手机号格式验证
				if (this.userInfo.phone && !/^1\d{10}$/.test(this.userInfo.phone)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					});
					return;
				}
				try {
					const result = await userApi.updateUserInfo(this.userInfo)
					uni.showLoading({
						title: '保存中...'
					});
					uni.showLoading({
						title: '保存成功',
						icon: 'success'
					});
					setTimeout(() => {
						uni.hideLoading()
						uni.switchTab({
							url: '/pages/mine/mine'
						})
					}, 700)
				} catch (error) {
					console.error('保存失败:', error);
					uni.hideLoading();
					uni.showToast({
						title: '保存失败: ' + (error.message || '未知错误'),
						icon: 'none'
					});
				}
			},
			goShop() {
				uni.switchTab({
					url: '/pages/shop/shop'
				})
			},
			goIndex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			goForum() {
				uni.switchTab({
					url: '/pages/forum/forum'
				})
			},
			goMine() {
				uni.switchTab({
					url: '/pages/mine/mine'
				})
			},
			goHealth() {
				uni.switchTab({
					url: '/pages/health/health'
				})
			},
			goShopCar() {
				uni.switchTab({
					url: '/pages/shop/shopCar'
				})
			},
		}
	};
</script>

<style scoped>
	/* 顶部导航栏样式 */
	.top-navbar {
		background-color: white;
		padding: 15px 0;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 1000;
	}

	.logo {
		display: flex;
		align-items: center;
	}

	.logo image {
		width: 60px;
		height: 60px;
		margin-right: 15px;
	}

	.logo .titleText {
		font-size: 24px;
		font-weight: bold;
		color: #000000;
	}

	.navbar-center {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.nav-categories {
		display: flex;
	}

	.nav-item {
		padding: 8px 15px;
		color: #000000;
		font-size: 20px;
		font-weight: bold;
		cursor: pointer;
		margin-right: 10px;
		border-radius: 5px;
		transition: all 0.3s;
	}

	.nav-item:hover {
		background-color: rgba(255, 255, 255, 0.2);
	}

	.search-container {
		display: flex;
		align-items: center;
		background-color: rgba(255, 255, 255, 0.8);
		border-radius: 50px;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
		max-width: 300px;
	}

	.search-container image {
		width: 20px;
		height: 20px;
		margin-left: 10px;
	}

	.search-input {
		flex: 1;
		border: none;
		outline: none;
		font-size: 15px;
		padding: 5px 5px;
		background-color: transparent;
	}

	.search-btn {
		background-color: #F9CB43;
		color: #fff;
		border: none;
		border-radius: 0 50px 50px 0;
		padding: 5px 15px;
		font-size: 14px;
		cursor: pointer;
	}

	.user-actions {
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.action-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-left: 20px;
		cursor: pointer;
	}

	.action-item image {
		width: 24px;
		height: 24px;
	}

	.action-item .titleText {
		margin-top: 5px;
		color: #000000;
		font-size: 14px;
	}

	.page-container {
		padding: 40px;
		background-color: #f5f5f5;
		min-height: 100vh;
		display: flex;
		justify-content: center;
	}

	.info-container {
		background-color: white;
		border-radius: 12px;
		padding: 40px;
		height: 59%;
		box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
		width: 80%;
		max-width: 900px;
		margin-top: 270rpx;
	}

	.info-content {
		display: flex;
		margin-bottom: 40px;
	}

	/* 左侧头像部分 */
	.avatar-section {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-right: 60px;
		border-right: 1px solid #eaeaea;
		width: 220px;
	}

	.avatar {
		width: 160px;
		height: 160px;
		border-radius: 50%;
		margin-bottom: 20px;
		object-fit: cover;
		box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
		transition: transform 0.3s ease;
		cursor: pointer;
	}

	.avatar:hover {
		transform: scale(1.05);
	}

	.edit-avatar {
		font-size: 14px;
		color: #F9CB43;
		cursor: pointer;
		transition: all 0.3s ease;
		padding: 6px 12px;
		border-radius: 16px;
		background-color: rgba(249, 203, 67, 0.1);
	}

	.edit-avatar:hover {
		background-color: rgba(249, 203, 67, 0.2);
	}

	/* 右侧表单部分 */
	.form-section {
		flex: 1;
		padding-left: 60px;
	}

	.form-item {
		margin-bottom: 25px;
	}

	.label {
		display: block;
		font-size: 14px;
		color: #666;
		margin-bottom: 8px;
		font-weight: 500;
	}

	.input {
		width: 100%;
		height: 44px;
		background-color: #f7f7f7;
		border-radius: 8px;
		padding: 0 15px;
		font-size: 14px;
		border: 1px solid #eaeaea;
		transition: all 0.3s ease;
	}

	.input:focus,
	.input:hover {
		border-color: #F9CB43;
		background-color: #fffcf0;
	}

	/* 底部按钮部分 */
	.button-section {
		display: flex;
		justify-content: flex-end;
	}

	.save-btn {
		width: 180px;
		height: 44px;
		background-color: #F9CB43;
		color: white;
		border-radius: 8px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 16px;
		font-weight: 500;
		transition: all 0.3s ease;
		border: none;
		cursor: pointer;
	}

	.save-btn:hover {
		background-color: #eabe35;
		transform: translateY(-2px);
		box-shadow: 0 4px 12px rgba(249, 203, 67, 0.3);
	}

	/* 响应式设计 */
	@media screen and (max-width: 768px) {
		.info-content {
			flex-direction: column;
		}

		.avatar-section {
			width: 100%;
			border-right: none;
			border-bottom: 1px solid #eaeaea;
			padding-right: 0;
			padding-bottom: 30px;
			margin-bottom: 30px;
		}

		.form-section {
			padding-left: 0;
		}

		.info-container {
			width: 95%;
			padding: 30px;
		}

		.button-section {
			justify-content: center;
		}

		.save-btn {
			width: 100%;
		}
	}
</style>